package droid.insane.GUI;

import droid.insane.database.DBHandler;
import droid.insane.logic.ConstantValues;
import droid.insane.logic.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.TextView;

public class CompanyInfo extends Activity {
	private String symbol;
	private Handler handler;

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.companygeneralinfo);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			symbol = extras.getString(ConstantValues.SYMBOL);
		}

		final TextView edtSymbol = (TextView) findViewById(R.id.txvTitle);
		final TextView edtName = (TextView) findViewById(R.id.txvDescription);
		final TextView edtOpen = (TextView) findViewById(R.id.txvOpen);
		final TextView edtHigh = (TextView) findViewById(R.id.txvHigh);
		final TextView edtLow = (TextView) findViewById(R.id.txvLow);
		final TextView edtPrevClose = (TextView) findViewById(R.id.txvPreviousClose);
		final TextView edtLastTraded = (TextView) findViewById(R.id.txvLastTraded);
		final TextView edtChange = (TextView) findViewById(R.id.txvChange);
		final TextView edtAvPrice = (TextView) findViewById(R.id.txvMyPrice);
		final TextView edtQuantity = (TextView) findViewById(R.id.txvMyQty);
		final TextView edtProfitLoss = (TextView) findViewById(R.id.txvMyProfit);

		handler = new Handler() {
			/* (non-Javadoc)
			 * @see android.os.Handler#handleMessage(android.os.Message)
			 */
			@Override
			public void handleMessage(Message msg) {
				Bundle b = msg.getData();
				String[] temp = b.getStringArray(ConstantValues.DATA);

				edtSymbol.setText(temp[0]);
				edtName.setText(temp[1]);
				edtOpen.setText(temp[2]);
				edtHigh.setText(temp[3]);
				edtLow.setText(temp[4]);
				edtPrevClose.setText(temp[5]);
				edtLastTraded.setText(temp[6]);
				edtChange.setText(temp[7]);
				edtAvPrice.setText(temp[8]);
				edtQuantity.setText(temp[9]);
				edtProfitLoss.setText(temp[10]);
			}
		};
		getInformation();

	}

	/**
	 * retreives data from the database
	 */
	private void getInformation() {
		Thread t = new Thread() {
			@Override
			public void run() {
				Message message = new Message();
				Bundle b = new Bundle();
				DBHandler dbHandler = DBHandler.getDBHandler(null);
				try {
					dbHandler.openToRead();
					String[] arr = dbHandler.getCompanyDetails(symbol);
					dbHandler.close();

					b.putStringArray(ConstantValues.DATA, arr);
					message.setData(b);
					handler.sendMessage(message);

				} catch (Exception e) {
					Log.e(ConstantValues.TAG, e.getMessage());
				}

			};
		};
		t.start();

	}

}
